from django.core.validators import RegexValidator
from app01 import models
from app01.utils.MyModelForm import BootStrapModelForm
from django import forms
from django.core.validators import ValidationError
from app01.utils.encrypt import md5


class UserChangePasswordForm(BootStrapModelForm):
    """用户修改密码"""
    User_password = forms.CharField(
        label="用户密码(至少八个字符，至少一个字母和一个数字)",
        validators=[RegexValidator("^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$")]
    )
    confirm_password = forms.CharField(
        label='确认密码',
        widget=forms.PasswordInput
    )

    class Meta:
        model = models.User
        fields = ["User_password"]

    def clean_confirm_password(self):
        confirm = md5(self.cleaned_data.get('confirm_password'))
        # confirm = md5(confirm)#加密所需
        pwd = self.cleaned_data.get('User_password')
        if confirm != pwd:
            raise ValidationError("密码不一致，请重新输入")
        # 钩子方法的返回值可以写到数据库中
        return confirm

    def clean_User_password(self):
        pwd = self.cleaned_data.get('User_password')
        return md5(pwd)


class UserEditModelForm(BootStrapModelForm):
    """用户信息修改"""
    User_name = forms.CharField(
        label="用户名(至少六个字符)",
        disabled=True
    )

    User_email = forms.CharField(
        label="用户邮箱",
        max_length=64,
        validators=[RegexValidator("^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")]
    )

    class Meta:
        model = models.User
        exclude = ["User_account", "User_password"]
        # 手动添加标签
        # widgets={
        #     "User_name":forms.TextInput(attrs={"class":"form-control"})
        # }

    def clean_User_password(self):
        pwd = self.cleaned_data.get("User_password")
        return md5(pwd)


class UserAddModelForm(BootStrapModelForm):
    """用户注册表"""
    User_name = forms.CharField(
        label="用户名(至少六个字符)",
        min_length=6
    )
    User_password = forms.CharField(
        label="用户密码(至少八个字符，至少一个字母和一个数字)",
        validators=[RegexValidator("^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$")]
    )
    confirm_password = forms.CharField(
        label='确认密码',
        widget=forms.PasswordInput
    )

    User_email = forms.CharField(
        label="用户邮箱",
        max_length=64,
        validators=[RegexValidator("^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")]
    )

    class Meta:
        model = models.User
        exclude = ['User_account']
        # 手动添加标签
        # widgets={
        #     "User_name":forms.TextInput(attrs={"class":"form-control"})
        # }

    def clean_User_name(self):
        txt_name = self.cleaned_data["User_name"]
        if models.User.objects.filter(User_name=txt_name).exists():
            raise ValidationError("用户名已存在")
        return txt_name

    def clean_confirm_password(self):
        confirm = md5(self.cleaned_data.get('confirm_password'))
        # confirm = md5(confirm)#加密所需
        pwd = self.cleaned_data.get('User_password')
        if confirm != pwd:
            raise ValidationError("密码不一致，请重新输入")
        # 钩子方法的返回值可以写到数据库中
        return confirm

    def clean_User_password(self):

        pwd = self.cleaned_data.get('User_password')
        return md5(pwd)


class ManagerAddModelForm(BootStrapModelForm):
    """管理员注册表"""
    confirm_password = forms.CharField(
        label='确认密码',
        widget=forms.PasswordInput
    )
    Manager_password = forms.CharField(
        label='密码',
        widget=forms.PasswordInput
    )

    class Meta:
        model = models.Manager
        fields = "__all__"
        # 手动添加标签
        # widgets={
        #     "User_name":forms.TextInput(attrs={"class":"form-control"})
        # }

    def clean_confirm_password(self):
        confirm = md5(self.cleaned_data.get('confirm_password'))
        # confirm = md5(confirm)  # 加密所需
        pwd = self.cleaned_data.get('Manager_password')
        print('pwd', pwd)
        print('confirm', confirm)
        if confirm != pwd:
            raise ValidationError("密码不一致，请重新输入")
        # 钩子方法的返回值可以写到数据库中
        return confirm

    # 密文存储(加密)
    def clean_Manager_password(self):
        pwd = self.cleaned_data.get("Manager_password")
        return md5(pwd)

    def clean_Manager_name(self):
        txt_name = self.cleaned_data["Manager_name"]
        if models.Manager.objects.filter(Manager_name=txt_name).exists():
            raise ValidationError("用户名已存在")
        return txt_name


class UserLoginModelForm(BootStrapModelForm):
    """用户登录表"""
    User_name = forms.CharField(
        label="用户名",
        widget=forms.TextInput,
        required=True,
    )
    User_password = forms.CharField(
        label='密码',
        widget=forms.PasswordInput,
        required=True,
    )

    class Meta:
        model = models.User
        fields = ["User_name", "User_password"]

    def clean_User_password(self):
        pwd = self.cleaned_data.get('User_password')
        return md5(pwd)


class ManagerLoginModelForm(BootStrapModelForm):
    """管理员登录表"""
    Manager_name = forms.CharField(
        label="用户名",
        widget=forms.TextInput,
        required=True,
    )
    Manager_password = forms.CharField(
        label='密码',
        widget=forms.PasswordInput,
        required=True,
    )

    class Meta:
        model = models.Manager
        fields = ["Manager_name", "Manager_password"]

    def clean_Manager_password(self):
        pwd = self.cleaned_data.get('Manager_password')
        return md5(pwd)


class OrderModelForm(BootStrapModelForm):
    user = forms.CharField(disabled=True, label='用户')
    oid = forms.CharField(disabled=True, label='订单号')

    class Meta:
        model = models.RechargeOrder
        fields = ["price"]


class FunctionForm(BootStrapModelForm):
    class Meta:
        model = models.Record
        fields = "__all__"
